package test.lodsve.service;

import lodsve.validate.core.NeedValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import test.lodsve.config.TestProperties;
import test.lodsve.domain.User;
import test.lodsve.dto.UserDto;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Locale;

/**
 * .
 *
 * @author sunhao(sunhao.java @ gmail.com)
 * @version V1.0, 16/1/18 下午6:41
 */
@Service
public class UserService {
    @Autowired
    private TestProperties testProperties;

    public User loadUser(Long id) {
        return new User(testProperties.getUserName(), testProperties.getPassWord());
    }

    @NeedValidate
    public User save(UserDto userDto) {
        return new User(testProperties.getUserName(), testProperties.getPassWord());
    }

    public boolean auth(Long id, boolean authResult) {
        return true;
    }

    public boolean delete(Long id) {
        return true;
    }

    public User update(UserDto userDto) {
        return new User(testProperties.getUserName(), testProperties.getPassWord());
    }

    public Page<User> findAll(Pageable pageable) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Locale locale = LocaleContextHolder.getLocale();

        System.out.println(request + "" + locale);

        return new PageImpl<>(Collections.singletonList(new User(testProperties.getUserName(), testProperties.getPassWord())), pageable, 1);
    }
}
